package com.example.hao.day10_11_contentprovider;

import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.provider.CallLog;
import android.provider.ContactsContract;
import android.provider.Telephony;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public static final String TAG = "MainActivity";

    public void getCallLog(View view) {
        //访问通话记录的Uri
        Uri uri = Uri.parse("content://call_log/calls");
        //获取内容解析器
        ContentResolver cr = getContentResolver();

        Cursor cursor = cr.query(uri, null, null, null, null);

        /*
         * 查询通话记录信息表的相关列名：
         CallLog.Calls._ID：主键 ---int类型
         CallLog.Calls.NUMBER：电话号码--String类型
         CallLog.Calls.DATE：通话时间 ---long类型
         CallLog.Calls.TYPE：通话类型 ---int类型

         */
        if (cursor != null) {
            while (cursor.moveToNext()) {
                int id = cursor.getInt(cursor.getColumnIndex(CallLog.Calls._ID));
                int type = cursor.getInt(cursor.getColumnIndex(CallLog.Calls.TYPE));
                String number = cursor.getString(cursor.getColumnIndex(CallLog.Calls.NUMBER));
                long date = cursor.getLong(cursor.getColumnIndex(CallLog.Calls.DATE));

                Log.i(TAG, "getCallLog: id " + id);
                Log.i(TAG, "getCallLog: type " + type);
                Log.i(TAG, "getCallLog: number " + number);
                Log.i(TAG, "getCallLog: date " + date);
            }
            cursor.close();
        }
    }

    /*
    访问系统短信的URI：Uri.parse(“content://sms”);
    查询系统短信信息表的相关列名：
    Telephony.Sms.ADDRESS：短信号码 --字符串类型
    Telephony.Sms.BODY：短信内容 ---字符串类型
    Telephony.Sms.TYPE：短信类型 ---int类型
    Telephony.Sms.DATE：短信类型--long类型

     */
    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    public void getMsg(View view) {
        Uri uri = Uri.parse("content://sms");

        ContentResolver cr = getContentResolver();

        Cursor cursor = cr.query(uri, null, null, null, null);

        if (cursor != null) {
            while (cursor.moveToNext()) {
                String address = cursor.getString(cursor.getColumnIndex(Telephony.Sms.ADDRESS));
                String body = cursor.getString(cursor.getColumnIndex(Telephony.Sms.BODY));
                int type = cursor.getInt(cursor.getColumnIndex(Telephony.Sms.TYPE));
                long date = cursor.getLong(cursor.getColumnIndex(CallLog.Calls.DATE));

                Log.i(TAG, "getMsg: body " + body);
                Log.i(TAG, "getMsg: type " + type);
                Log.i(TAG, "getMsg: number " + address);
                Log.i(TAG, "getMsg: date " + date);
                Log.d(TAG, "getMsg:                     ");
            }
            cursor.close();
        }
    }


    public void getContacts(View view) {
        Uri parse = ContactsContract.RawContacts.CONTENT_URI;
        //获取内容解析器
        ContentResolver cr = getContentResolver();
        Cursor cursor = cr.query(parse, null, null, null, null);
        if (cursor != null) {
            while (cursor.moveToNext()) {
                //获取联系人主表的 联系人名字
                String name = cursor.getString(cursor.getColumnIndex("display_name"));
                //获取英文名称
                String englishName = cursor.getString(cursor.getColumnIndex("phonetic_name"));
                //获取联系人主表的 ID
                int id = cursor.getInt(cursor.getColumnIndex("contact_id"));

                //根据练习人的Id 去Data表里面查询联系人 更多信息(邮箱,电话号码,地址等...)
                Cursor dataCursor = cr.query(ContactsContract.Data.CONTENT_URI, null, "raw_contact_id = ?", new String[]{id + ""}, null);


                Log.w(TAG, "******************************" + name + "*********************************");
                Log.d(TAG, "getCallLog: " + name + " \t " + id + " \t " + englishName);

                if (dataCursor != null) {
                    while (dataCursor.moveToNext()) {

                        String data1 = dataCursor.getString(dataCursor.getColumnIndex("data1"));
                        String data2 = dataCursor.getString(dataCursor.getColumnIndex("data2"));
                        String data3 = dataCursor.getString(dataCursor.getColumnIndex("data3"));
                        String data4 = dataCursor.getString(dataCursor.getColumnIndex("data4"));
                        String data5 = dataCursor.getString(dataCursor.getColumnIndex("data5"));

                        //1:Email
                        //2:社交工具
                        //3:昵称
                        //4:公司信息
                        //5:通讯号码
                        //6:卫星电话
                        //7:名字
                        //8:地址
                        //12:备注
                        //14:网址
                        String mimetype = dataCursor.getString(dataCursor.getColumnIndex("mimetype"));

                        switch (mimetype) {
                            case "vnd.android.cursor.item/email_v2":
                                //data2:1个人邮箱;2企业邮箱
                                switch (data2) {
                                    case "1":
                                        Log.i(TAG, "getContacts: 个人邮箱" + data1);
                                        break;
                                    case "2":
                                        Log.i(TAG, "getContacts: 企业邮箱" + data1);
                                        break;
                                }
                                break;
                            case "vnd.android.cursor.item/im":
                                //data2:
                                //data5:
                                Log.i(TAG, "getContacts: 通讯账号" + data1);
                                break;
                            case "vnd.android.cursor.item/nickname":
                                Log.i(TAG, "getContacts: 昵称:" + data1);
                                break;
                            case "vnd.android.cursor.item/organization":
                                //data4 是 职位
                                Log.i(TAG, "getContacts: 公司名称:" + data1 + ", 职位:" + data4);
                                break;
                            case "vnd.android.cursor.item/phone_v2":
                                //data2:1家庭电话,2手机号码,3公司电话
                                switch (data2) {
                                    case "1":
                                        Log.i(TAG, "getContacts: 家庭电话:" + data1);
                                        break;
                                    case "2":
                                        Log.i(TAG, "getContacts: 手机号码:" + data1);
                                        break;
                                    case "3":
                                        Log.i(TAG, "getContacts: 公司电话:" + data1);
                                        break;
                                }
                                break;
                            case "vnd.android.cursor.item/sip_address"://卫星电话
                                Log.i(TAG, "getContacts: 卫星电话:" + data1);
                                break;
                            case "vnd.android.cursor.item/name"://名字
                                //data2:名字
                                //data3:姓氏
                                Log.i(TAG, "getContacts: 全名:" + data1);
                                Log.i(TAG, "getContacts: 名字:" + data2);
                                Log.i(TAG, "getContacts: 姓氏:" + data3);
                                break;
                            case "vnd.android.cursor.item/postal-address_v2"://地址
                                switch (data2) {
                                    case "1":
                                        Log.i(TAG, "getContacts: 住宅地址:" + data1);
                                        break;
                                    case "2":
                                        Log.i(TAG, "getContacts: 公司地址:" + data1);
                                        break;
                                }
                                break;
                            case "vnd.android.cursor.item/note"://备注
                                Log.i(TAG, "getContacts: 备注:" + data1);
                                break;
                            case "vnd.android.cursor.item/website"://网址
                                Log.i(TAG, "getContacts: 网址:" + data1);
                                break;
                        }
                    }
                    dataCursor.close();
                }


            }
            cursor.close();
        }


    }

    public void visitInsert(View view) {

        ContentResolver contentResolver = getContentResolver();
        Uri parse = Uri.parse("content://com.xykj.student/students");
        ContentValues contentValues = new ContentValues();
        contentValues.put("_age", 18);
        contentValues.put("_name", "张三");
        contentValues.put("_sex", "男");
        contentValues.put("_height", 183);
        Uri insert = contentResolver.insert(parse, contentValues);

        Log.d(TAG, "visitInsert: " + insert);
        long l = ContentUris.parseId(insert);
        Log.d(TAG, "visitInsert: " + l);
    }

    public void visitDelete(View view) {
        ContentResolver contentResolver = getContentResolver();
        Uri parse = Uri.parse("content://com.xykj.student/students");
        int delete = contentResolver.delete(parse, "_id = ?", new String[]{"1"});
        Log.d(TAG, "visitDelete: " + delete);
    }

    public void visitUpdate(View view) {
        ContentResolver contentResolver = getContentResolver();
        Uri parse = Uri.parse("content://com.xykj.student/students");

        ContentValues contentValues = new ContentValues();
        contentValues.put("_age", 18);
        contentValues.put("_name", "李四");
        contentValues.put("_sex", "女");
        contentValues.put("_height", 168);
        int update = contentResolver.update(parse, contentValues, "_id > ?", new String[]{"1"});

        Log.d(TAG, "visitUpdate: " + update);
    }

    public void visitQuery(View view) {
        ContentResolver contentResolver = getContentResolver();
        Uri parse = Uri.parse("content://com.xykj.student/students");
        Cursor query = contentResolver.query(parse, null, null, null, null);
        if (query != null) {
            while (query.moveToNext()) {
                String name = query.getString(query.getColumnIndex("_name"));
                int id = query.getInt(query.getColumnIndex("_id"));
                int age = query.getInt(query.getColumnIndex("_age"));
                String sex = query.getString(query.getColumnIndex("_sex"));
                double height = query.getDouble(query.getColumnIndex("_height"));
                Log.i(TAG, "visitQuery: " + name + "," + id + "," + age + "," + sex + "," + height);
            }
            query.close();
        }
    }
}
